/* 
 * SimpleBaseController.java      1.00    2008-01-11
 *
 * Copyright (c) 2010 leewan. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of leewan.  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with leewan.
 */
package doit.framework.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import doit.framework.util.AnnotationController;
import doit.framework.util.Constants;
import doit.framework.util.RedirectUtil;
import doit.framework.util.SharedMethods;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : SimpleBaseController.java
 * description : Service를 접근할 수 있는 Controller이며
 *               자동생성된 소스 이외의 Controller 작성 시 사용
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008-01-11  leewan  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")

public abstract class SimpleBaseController extends AbstractController {
	@Autowired
	private AnnotationController controllerExecuteChecker;	
	private String viewName;
		
	public String getViewName() {
		return viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	@Override
	protected ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response) throws Exception {
		String className = SharedMethods.getClassName( this.getClass());
		ModelAndView modelAndView = controllerExecuteChecker.annotation(request, response, className);
		modelAndView = controllerExecuteChecker.sessionChecker(request, response, className);

		if (modelAndView == null ) {
			modelAndView = execute( request, response);
		} 
		else {
			if (modelAndView.getViewName().indexOf("redirect:") < 0) {
				getMenu(modelAndView);
			} else {
				this.redirect(request, response, modelAndView.getModel(), modelAndView.getViewName().replaceAll("redirect:", ""));
				modelAndView = null;
			}
		}
		return modelAndView;
	}
	
	protected abstract ModelAndView execute( HttpServletRequest request, HttpServletResponse response) throws Exception;
	
	protected void redirect(HttpServletRequest request, HttpServletResponse response, Map model, String viewName) throws Exception
	{
		if (viewName != null)
			RedirectUtil.redirect(request, response, model, viewName);
		else
			response.sendRedirect("/");
	}	
	
	protected void getMenu(ModelAndView modelAndView) throws Exception {
	//	return modelAndView;
	}
	
	protected void bind( HttpServletRequest request, Map input) throws Exception{
       	Enumeration e = request.getParameterNames();			
       	while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			if (!request.getParameter(key).equals("") && request.getParameter(key) != null && !request.getParameter(key).equals("method"))
				input.put( key, request.getParameter(key) );
		}
       	String location = request.getRequestURI();
       	int getParamIdx = location.indexOf('?');
       	if (getParamIdx > 0) {
       		location = location.substring(0, getParamIdx);	
       	}
       	input.put( Constants.VL_LOCATION,request.getRequestURI());
       	
       	SharedMethods.bind(request, input);
	}
	
	protected void bind(HttpServletRequest request, Object input) throws Exception {
		Enumeration e = request.getParameterNames();			
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			if (request.getParameter(key).equals("") ||  request.getParameter(key) == null )
				request.removeAttribute(key);
		}
		ServletRequestDataBinder srBinder = new ServletRequestDataBinder( input);
		srBinder.bind( request);
		
		SharedMethods.bind(request, input);
	}
	
	protected boolean bindCheckBox(HttpServletRequest request, Map input, String iterateName) {
		boolean _ret = false;
		ArrayList aRchk = new ArrayList();
		String[] chk = request.getParameterValues( iterateName);
		if (chk != null && chk.length > 0) {
			for (int i = 0 ; i < chk.length ; i++) {
				aRchk.add(chk[i]);
			}
			input.put(iterateName, aRchk);
			_ret = true;
		}
		return _ret;
	}
	
	protected boolean bindCheckBox(HttpServletRequest request, Map input, String iterateName, String checkBoxName) {
		boolean _ret = false;
		ArrayList aRchk = new ArrayList();
		String[] chk = request.getParameterValues( checkBoxName);
		if (chk != null && chk.length > 0) {
			for (int i = 0 ; i < chk.length ; i++) {
				aRchk.add(chk[i]);
			}
			input.put(iterateName, aRchk);
			_ret = true;
		}
		return _ret;
	}
}
